//
//  CTCArticleDetailCell.m
//  ConsultantVersion
//
//  Created by  小龙 on 2016/12/12.
//  Copyright © 2016年 com.jieshuibao1. All rights reserved.
//

#import "CTCActivityIntroCell.h"
#import "CTCActivityModel.h"

@interface CTCActivityIntroCell ()<UIWebViewDelegate>

//@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//
//@property (weak, nonatomic) IBOutlet UIImageView *certificationImageView;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *certificationWidth;

//@property (weak, nonatomic) IBOutlet UILabel *personalDetailLabel;
//@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
//
//
//
//@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelWidth;
//
//@property (weak, nonatomic) IBOutlet UILabel *timelabel;
//@property (weak, nonatomic) IBOutlet UIButton *tagBtn;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewHeight;
//@property (weak, nonatomic) IBOutlet UIView *shareView;
//
@property (nonatomic, assign) CGFloat expectdHeight;

//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qWidth;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qzWidth;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wcWidth;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wtWidth;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *sinaWidth;
//
//
//
//@property (weak, nonatomic) IBOutlet UILabel *redListLabel;
//
//@property (weak, nonatomic) IBOutlet UIView *redListHeadView;
//
//@property (weak, nonatomic) IBOutlet UIButton *redListBtn;
//@property (weak, nonatomic) IBOutlet UIView *redPacketBtnView;
//@property (weak, nonatomic) IBOutlet UIView *redPacketListView;


@end


@implementation CTCActivityIntroCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
//    [self.deleteBtn setButtonImageTitleStyle:ButtonImageTitleStyleDefault padding:8];
    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    self.webView.multipleTouchEnabled = NO;
    self.webView.autoresizesSubviews = YES;
    self.webView.opaque = NO;
    self.webView.scrollView.scrollEnabled = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.webView.delegate = self;
    
    _expectdHeight = 0;
}

- (void)setHtml:(NSString *)html
{
    if ([_html isEqualToString:html]) return;
    
    _html = [html copy];
    DeLog(@"---- %@", html);
    if (!self.webView.isLoading && _html != nil && ![_html isEqualToString:@""]) {
        NSString *HTML = [_html stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
        [self.webView loadHTMLString:HTML baseURL:nil];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if(!webView.isLoading){
        
        CGFloat height = [webView sizeThatFits:CGSizeZero].height + 10;
        if (height != _webViewHeight.constant) {
            
            _webViewHeight.constant = height;
            _expectdHeight = height;
//            DeLog(@"height %f", height);
            if (self.delegate && [self.delegate respondsToSelector:@selector(activityIntroCell:heightDidChange:)]) {
                [self.delegate activityIntroCell:self heightDidChange:height];
            }
        }
    }
    
    
}
    
//
//    CGFloat height00 = [webView sizeThatFits:CGSizeZero].height;
////    CGFloat height01 = webView.scrollView.contentSize.height;
////    CGFloat height02 = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
////    CGFloat height03 = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
//
////    DeLog(@"--height00 %f, height01 %f, height02 %f, height03 %f", height00, height01, height02, height03);



- (void)stopLoading
{
    if ([self.webView isLoading]) {
        [self.webView stopLoading];
    }
    
    [self cleanCacheAndCookie];
}

- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
}


//- (IBAction)briberyBtnClicked:(UIButton *)sender {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(articleDetailCellbriberyBtnDidClicked:)]) {
//        [self.delegate articleDetailCellbriberyBtnDidClicked:self];
//    }
//}


//- (IBAction)headCoverBtnClicked:(UIButton *)sender {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(originalDetailCellHeadCoverBtnDidClicked)]) {
//        [self.delegate originalDetailCellHeadCoverBtnDidClicked];
//    }
//}

//- (IBAction)deleteBtnClick {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(articleDetailCellDeleteBtnClick:)]) {
//        [self.delegate articleDetailCellDeleteBtnClick:self];
//    }
//}


@end
